﻿using Hopscotch.Presentation.Common;
using System.Diagnostics;
using System.Globalization;

namespace Hopscotch.Presentation.DiagnosticsModule.Models
{
    
    public class SystemDiagnosticsModel : ModelBase
    {
        private string threadId;
        private string physicalMemoryUsage;
        private string userProcessorTime;
        private string totalProcessorTime;

        private string peakWorkingSet;
        private string peakPagedMemorySize;
        private string peakVirtualMemorySize;

        public SystemDiagnosticsModel(int threadId)
        {
            ThreadId = threadId.ToString(CultureInfo.InvariantCulture);
        }

        public string PhysicalMemoryUsage
        {
            get { return physicalMemoryUsage; }
            set { SetProperty(ref physicalMemoryUsage, value); }
        }

        public string UserProcessorTime
        {
            get { return userProcessorTime; }
            set { SetProperty(ref userProcessorTime, value); }
        }

        public string TotalProcessorTime
        {
            get { return totalProcessorTime; }
            set { SetProperty(ref totalProcessorTime, value); }
        }

        public string PeakWorkingSet
        {
            get { return peakWorkingSet; }
            set { SetProperty(ref peakWorkingSet, value); }
        }

        public string PeakPagedMemorySize
        {
            get { return peakPagedMemorySize; }
            set { SetProperty(ref peakPagedMemorySize, value); }
        }

        public string PeakVirtualMemorySize
        {
            get { return peakVirtualMemorySize; }
            set { SetProperty(ref peakVirtualMemorySize, value); }
        }

        public string ThreadId
        {
            get { return threadId; }
            set { SetProperty(ref threadId, value); }
        }


        public void Update()
        {
            var currentProcess = Process.GetCurrentProcess();

            PhysicalMemoryUsage = currentProcess.WorkingSet64.ToString("N0");
            UserProcessorTime = currentProcess.UserProcessorTime.ToString(@"hh\:mm\:ss");
            TotalProcessorTime = currentProcess.TotalProcessorTime.ToString(@"hh\:mm\:ss");
            PeakPagedMemorySize = currentProcess.PeakPagedMemorySize64.ToString("N0");
            PeakVirtualMemorySize = currentProcess.PeakVirtualMemorySize64.ToString("N0");
            PeakWorkingSet = currentProcess.PeakWorkingSet64.ToString("N0");
        }
    }
}
